package com.huanying.framework.building;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.huanying.framework.BaseController;
import com.huanying.framework.parameter.Parameter;
import com.huanying.framework.parameter.ParameterService;
@Controller
public class SaleController extends BaseController {
	@Autowired
	private SaleService saleService;
	@Autowired
	private ParameterService parameterService;
	
	private static Logger logger = Logger.getLogger(SaleController.class);
	
	@RequestMapping("/show_sale.do")
	public String show_list(Model model) throws Exception {
		parameterService.intParamObject();
		List paraList = Parameter.getPara_object();
		Gson gson = new Gson();
		String paraJson = gson.toJson(paraList);
		model.addAttribute("paraJson",paraJson);
		return "sale/add_sale";
	}
	
	@RequestMapping("/show_add_sale.do")
	public String show_add(Model model) throws Exception {
		parameterService.intParamObject();
		List paraList = Parameter.getPara_object();
		Gson gson = new Gson();
		String paraJson = gson.toJson(paraList);
		model.addAttribute("paraJson",paraJson);
		return "sale/add_sale";
	}
	
	@RequestMapping(value="/add_sale.do", method = RequestMethod.POST)
	@ResponseBody 
	public Map<String, String> save(@RequestBody Sale sale) {
		Map<String, String> map = new HashMap<String, String>();
		try {
			if(sale.getId()==0){
				saleService.add(sale);
			}
			else{
				saleService.update(sale);
			}
			map.put("flag", "1");
			map.put("id", String.valueOf(sale.getId()));
		} catch (Exception e) {
			logger.info("添加销售人员出错",e);
			logger.error("添加房源失败",e);
			logger.debug("1111");
			
			map.put("flag", "0");
		}
		return map;
	}
	
	public static void main(String[] args) {
		System.out.println(UUID.randomUUID());
	}
}
